var express = require('express');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();
var path = require('path');
/* GET users listing. */
router.get('/', function(req, res, next) {
    if(req.query.test_login== "1"){
        if(req.session.username == undefined || req.session.username == "")
            res.send('0');
    }else if(req.query.login_out== "1"){//退出登录
        req.session.username="";
        res.send('1');
    }
    else{
        var db=new sqlite3.Database(path.join(__dirname, "../db/LH.s3db"));
        try{
            sqlStr1 = "SELECT s_name from student Where s_id = ? and password = ?";
            sqlStr2 = "SELECT com_name  from commitee Where com_id = ? and password = ?";
            sqlStr3 = "SELECT ct_name  from class_teacher Where ct_id = ? and password = ?";
            sqlStr4 = "SELECT m_name  from manager Where m_id = ? and password = ?";
            db.all(sqlStr1, [req.query.username,req.query.password],function(err, rows){
                if(rows.length==0){
                    db.all(sqlStr4, [req.query.username,req.query.password],function(err, rows){
                        if(rows.length==0){
                            db.all(sqlStr2, [req.query.username,req.query.password],function(err, rows){
                                if(rows.length==0){
                                    db.all(sqlStr3, [req.query.username,req.query.password],function(err, rows){
                                        if(rows.length==0){
                                            res.send('0');
                                        }else{
                                            req.session.username = req.query.username;
                                            data={name:rows[0]['ct_name'],tag:3};
                                            res.send(data);
                                        }
                                    } );
                                }else{
                                    req.session.username = req.query.username;
                                    data={name:rows[0]['com_name'],tag:2};
                                    res.send(data);
                                }
                            } );
                        }else{
                            req.session.username = req.query.username;
                            if(req.query.username.substring(2,4)=="01")//是学生会
                                data={name:rows[0]['m_name'],tag:5};
                            else
                                data={name:rows[0]['m_name'],tag:4};
                            res.send(data);
                        }
                    } );
                }else{
                    req.session.username = req.query.username;
                    data={name:rows[0]['s_name'],tag:1};
                    res.send(data);
                }
            } );
        }catch(ex){
            console.log(ex);
        }
    }
});

module.exports = router;
